package com.hellobike.haxing.modules.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.common.system.query.QueryGenerator;
import com.hellobike.haxing.common.util.oConvertUtils;
import com.hellobike.haxing.modules.product.entity.Product;
import com.hellobike.haxing.modules.product.entity.ProductTag;
import com.hellobike.haxing.modules.product.service.IProductService;
import com.hellobike.haxing.modules.product.service.IProductTagService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import com.hellobike.haxing.common.system.base.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.hellobike.haxing.common.annotation.AutoLog;
 /**
 * @Description: 商品标签
 * @Author: haxing-boot
 * @Date:   2020-02-12
 * @Version: V1.0
 */
@RestController
@RequestMapping("/product/productTag")
@Slf4j
public class ProductTagController extends BaseController<ProductTag, IProductTagService> {
	@Autowired
	private IProductTagService productTagService;
	@Autowired
 	private IProductService productService;
	
	/**
	 * 分页列表查询
	 *
	 * @param productTag
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "商品标签-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ProductTag productTag,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
//		QueryWrapper<ProductTag> queryWrapper = QueryGenerator.initQueryWrapper(productTag, req.getParameterMap());
		QueryWrapper<ProductTag> queryWrapper = new QueryWrapper<>(productTag);
		queryWrapper.orderByAsc("sort_index");
		Page<ProductTag> page = new Page<ProductTag>(pageNo, pageSize);
		IPage<ProductTag> pageList = productTagService.page(page, queryWrapper);
		if (pageList.getRecords().size() > 0) {
			List<Long> productIdList = pageList.getRecords().stream().map(ProductTag::getProductId).collect(Collectors.toList());
			List<Product> productList = productService.getByIds(productIdList);
			Map<Long, Product> productMap = productList.stream().collect(Collectors.toMap(Product::getId, Function.identity()));
			pageList.getRecords().forEach(item -> item.setProduct(productMap.get(item.getProductId())));
		}
		return Result.ok(pageList);
	}

	 /**
	  * 添加tag商品
	  * @param tagId
	  * @param productIds
	  * @return
	  */
	@GetMapping("/addTagProducts")
	 public Result<?> addTagProducts(@RequestParam(name="tagId",required=true) Long tagId,
									 @RequestParam(name="productIds",required=true) String productIds) {
		 return productTagService.addTagProducts(tagId, Arrays.asList(productIds.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList()));
	 }
	
	/**
	 *   添加
	 *
	 * @param productTag
	 * @return
	 */
	@AutoLog(value = "商品标签-添加",operateType=2)
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ProductTag productTag) {
		productTagService.save(productTag);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param productTag
	 * @return
	 */
	@AutoLog(value = "商品标签-编辑",operateType=3)
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ProductTag productTag) {
		productTagService.updateById(productTag);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "商品标签-删除",operateType=4)
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) Long id) {
		productTagService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "商品标签-批量删除",operateType=4)
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.productTagService.removeByIds(Arrays.asList(ids.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList()));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) Long id) {
		ProductTag productTag = productTagService.getById(id);
		if(productTag==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(productTag);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param productTag
    */
    @AutoLog(value = "商品标签-导出excel",operateType=6)
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ProductTag productTag) {
        return super.exportXls(request, productTag, ProductTag.class, "商品标签");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @AutoLog(value = "商品标签-导入excel",operateType=5)
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ProductTag.class);
    }

}
